// packageE/picUpCard/picUpCardReimburse/picUpCardReimburse.js

var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    img_url_1: "",
    img_url_2: "",
    img_url_3: "",
    agreement: false,
    th_pass: "",
    th_name: "",
    order_id: 0,
    examine_success: false,
    configInfo: {},
    plugin: "picking-card",
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.orderid && options.orderid != 0 && options.orderid != null) {
      this.data.order_id = options.orderid;
    }
    if (options.plugin && options.plugin == "one") {
      //#78882，提货卡一期独立的新插件picking-card-one
      this.data.plugin = 'picking-card-one';
      wx.setNavigationBarTitle({
        title: '保证金退款'
      });
    }
    this.getConfigInfo();
    this.getExamineInfo();
  },

  //获取审核信息
  getExamineInfo() {
    let _api = "plugin.picking-card.frontend.pickingCard.checkRefundStatus";
    if (this.data.plugin == "picking-card-one") _api = "plugin.picking-card-one.frontend.pickingCard.checkRefundStatus";
    let urlStr = app.getNetAddresss(_api);
    app._getNetWork({
      url: urlStr,
      data: {
        order_id: this.data.order_id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          if (res.data === 0) {
            this.setData({
              examine_success: true,
            });
          }
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  getConfigInfo() {
    let _api = "plugin.picking-card.frontend.pickingCard.getConfig";
    if (this.data.plugin == "picking-card-one") _api = "plugin.picking-card-one.frontend.pickingCard.getConfig";
    let urlStr = app.getNetAddresss(_api);
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          this.setData({
            configInfo: res.data,
          });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  setExplainFlag() {
    this.setData({
      explainFlag: !this.data.explainFlag,
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  send() {
    let {
      th_pass,
      th_name,
      img_url_1,
      img_url_2,
      img_url_3,
      agreement,
      order_id,
    } = this.data;
    if (order_id == 0) return this.tips("参数错误");
    if (th_name == "") return this.tips("原订单收件人姓名不能为空");
    if (th_pass == "") return this.tips("原订单收件人手机号不能为空");
    if (img_url_1 == "" || img_url_2 == "" || img_url_3 == "")
      return this.tips("凭证必须为三张");
    if (agreement == false) return this.tips("请勾选退款说明");
    let _api = "plugin.picking-card.frontend.pickingCard.postRefund";
    if (this.data.plugin == "picking-card-one") _api = "plugin.picking-card-one.frontend.pickingCard.postRefund";
    let urlStr = app.getNetAddresss(_api);
    app._postNetWork({
      url: urlStr,
      data: {
        order_id,
        realname: th_name,
        mobile: th_pass,
        password: this.data.th_pass,
        images: img_url_1 + "," + img_url_2 + "," + img_url_3,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) {
          return this.tips(res.msg);
        }
        this.setData({ examine_success: true });
        console.log(res);
        // wx.redirectTo({
        // 	url: '/packageD/buy/orderPay/orderPay?order_id=' + res.data.order_ids + '&status=2'
        // })
        //packageD/buy/orderPay/orderPay
      },
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  onChange(evt) {
    this.setData({
      agreement: evt.detail,
    });
  },
  onRead(evt) {
    let index = evt.currentTarget.dataset.index;
    console.log(index);
    let that = this;
    let wx_token = wx.getStorageSync('wx_token');
    let session = wx.getStorageSync('session');
    let _api = "plugin.picking-card.frontend.pickingCard.upload";
    if (this.data.plugin == "picking-card-one") _api = "plugin.picking-card-one.frontend.pickingCard.upload";
    let urlStr = app.getNetAddresss(_api);
    urlStr += '&3rd_session=' + session;
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: "上传中",
        });
        wx.uploadFile({
          url: urlStr,
          filePath: tempFilePaths[0],
          name: "file",
          header:{
            "Content-Type": "application/json",
            Accept: "application/json",
            Cookie: "PHPSESSID=" + wx_token,
          },
          success(resdata) {
            wx.hideLoading();
            var res = JSON.parse(resdata.data);
            console.log(res);
            if (res.result == 1) {
              let key = "img_url_" + index;
              that.setData({
                [key]: res.data.img,
              });
            } else {
              wx.showToast({
                icon: "none",
                title: res.msg,
                duration: 1500,
              });
            }
          },
        });
      },
    });
  },
  tips(msg) {
    wx.showToast({
      icon: "none",
      title: msg,
    });
    return false;
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
